
#include <sourcemod>
#include <sdktools>

#include <l4d_stocks>
#include "macros.sp"
#include "helpers.sp"


//Handles
new Handle:cc_plpTimer = INVALID_HANDLE;


//CVARS
new plpTimer;


//Plugin start
public OnPluginStart()
{

	//Reg Commands
	RegConsoleCmd("players", PrintTeamsToClient);

	//Reg Cvars
	cc_plpTimer = CreateConVar("l4d_plp_timer", "20", "How long, in seconds, the Playerlist Panel stay before it close automatic");
	
	//Execute the config file
	AutoExecConfig(true, "l4d_teamspanel");
}


BuildPrintAllPanel(client)
{
	//Build panel
	new Handle:TeamPanel = CreatePanel();
	SetPanelTitle(TeamPanel, "## Players ##");
	new count;
	new total;
	new String:text[128];

	//Get & Draw Survivor Player Names
	total = 0;
	count = 0;
	
	
	DrawPanelText(TeamPanel, "+Spectators:");
	FOR_EACH_HUMAN_SPECTATOR(i)
	{
		Format(text, sizeof(text), "  %N", i);
		DrawPanelText(TeamPanel, text);
		count++;
	}
	Format(text, sizeof(text), "===> %d", count);
	DrawPanelText(TeamPanel, text);

	DrawPanelText(TeamPanel, "\n");

	total = count;
	count = 0;
	
	DrawPanelText(TeamPanel, "+Survivors:");
	FOR_EACH_SURVIVOR_HUMAN(i)
	{
		Format(text, sizeof(text), "  %N", i);
		DrawPanelText(TeamPanel, text);
		count++;
	}

	Format(text, sizeof(text), "===> %d/10", count);
	DrawPanelText(TeamPanel, text);

	DrawPanelText(TeamPanel, "\n");

	total = total + count;
	count = 0;

	DrawPanelText(TeamPanel, "+Infected:");
	FOR_EACH_INFECTED_HUMAN(i)
	{
		Format(text, sizeof(text), "  %N", i);
		DrawPanelText(TeamPanel, text);
		count++;
	}
	Format(text, sizeof(text), "===> %d/10", count);
	DrawPanelText(TeamPanel, text);

	DrawPanelText(TeamPanel, "\n");

	total = total + count;

	Format(text, sizeof(text), "Total: %d/20", total);
	DrawPanelText(TeamPanel, text);

	SendPanelToClient(TeamPanel, client, TeamPanelHandler, plpTimer);
	CloseHandle(TeamPanel);
}


BuildPrintSurvPanel(client)
{
	//Build panel
	new Handle:TeamPanel = CreatePanel();
	SetPanelTitle(TeamPanel, "## Survivors ##");
	new count;
	new String:text[128];

	//Get & Draw Survivor Player Names
	count = 0;
	
	FOR_EACH_SURVIVOR_HUMAN(i)
	{
		Format(text, sizeof(text), "  %N", i);
		DrawPanelText(TeamPanel, text);
		count++;
	}

	DrawPanelText(TeamPanel, "\n");
	Format(text, sizeof(text), "Total: %d/10", count);
	DrawPanelText(TeamPanel, text);
	SendPanelToClient(TeamPanel, client, TeamPanelHandler, plpTimer);
	CloseHandle(TeamPanel);

}

BuildPrintInfPanel(client)
{
	//Build panel
	new Handle:TeamPanel = CreatePanel();
	SetPanelTitle(TeamPanel, "## Infected ##");
	new count;
	new String:text[128];
	
	//Get & Draw Infected Player Names
	count = 0;
	
	FOR_EACH_INFECTED_HUMAN(i)
	{
		Format(text, sizeof(text), "  %N", i);
		DrawPanelText(TeamPanel, text);
		count++;
	}

	DrawPanelText(TeamPanel, "\n");
	Format(text, sizeof(text), "Total: %d/10", count);
	DrawPanelText(TeamPanel, text);
	SendPanelToClient(TeamPanel, client, TeamPanelHandler, plpTimer);
	CloseHandle(TeamPanel);

}

BuildPrintSpecPanel(client)
{
	//Build panel
	new Handle:TeamPanel = CreatePanel();
	SetPanelTitle(TeamPanel, "## Spectators ##");
	new count;
	new String:text[128];
	
	//Get & Draw Spec Player Names
	count = 0;
	
	FOR_EACH_HUMAN_SPECTATOR(i)
	{
		Format(text, sizeof(text), "  %N", i);
		DrawPanelText(TeamPanel, text);
		count++;
	}

	DrawPanelText(TeamPanel, "\n");
	Format(text, sizeof(text), "Total: %d/10", count);
	DrawPanelText(TeamPanel, text);

	SendPanelToClient(TeamPanel, client, TeamPanelHandler, plpTimer);
	CloseHandle(TeamPanel);

}



//TeamPanelHandler
public TeamPanelHandler(Handle:TeamPanel, MenuAction:action, param1, param2)
{
	
}




//Send the Panel to the Client
public Action:PrintTeamsToClient(client, args)
{	
	decl String:full[8];
	GetCmdArgString(full, sizeof(full));
	if(strcmp(full, "surv") == 0)
	{
		plpTimer = GetConVarInt(cc_plpTimer);
		BuildPrintSurvPanel(client);
	}	
	else if(strcmp(full, "inf") == 0)
	{
		plpTimer = GetConVarInt(cc_plpTimer);
		BuildPrintInfPanel(client);
	}	
	else if(strcmp(full, "spec") == 0)
	{
		plpTimer = GetConVarInt(cc_plpTimer);
		BuildPrintSpecPanel(client);
	}
	else
	{
		plpTimer = GetConVarInt(cc_plpTimer);
		BuildPrintAllPanel(client);
	}


}
